package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

public class deleteBike extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");	
			}
			catch(Exception e) {}
		}
	public  void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String bid= req.getParameter("number");//获取需要删除的单车编号
		String distract= req.getParameter("distract");
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		Connection con = null;
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b) {
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
			PreparedStatement sql;
			//修改单车状态，删除订单表里的记录
			try {//连接数据库
				con = DriverManager.getConnection(url,"root","744615");
				String condition="delete from bike where B_id='"+bid+"'";//删除单车信息
				sql=con.prepareStatement(condition); 
				con.setAutoCommit(false);
				sql.executeUpdate();                  
				sql.close(); 
				condition="select * from district where Dname='"+distract+"'";//查询地区的单车数量 
				sql=con.prepareStatement(condition);
				ResultSet rs=sql.executeQuery(); 
				String num="";
				while(rs.next())
					num = rs.getString("DBnum");
				sql.close();
				condition="update district set DBnum=?-1 where Dname='"+distract+"'";//地区的单车数量-1
				sql=con.prepareStatement(condition);
				sql.setString(1, num); 
				sql.executeUpdate();                  
				sql.close(); 
	            con.commit();//进行手动提交（commit） 
				con.setAutoCommit(true); //提交完成后回复现场将Auto commit,还原为true	
			}
			catch (SQLException exp){
				System.out.println(exp);
				out.println("<script language = javascript>alert('更新数据库失败，删除失败!');");
				out.println("location.href='lookBikes.jsp'</script>");
				try {   
					 // 若出现异常，对数据库中所有已完成的操作全部撤销，则回滚到事务开始状态   
					if(!con.isClosed()){   
					 con.rollback();//4,当异常发生执行catch中SQLException时，记得要rollback(回滚)；   
					 System.out.println("更新数据库失败，回滚！");
					 con.setAutoCommit(true);   
					 }   
					} 
				 catch (SQLException e1) {   
					 e1.printStackTrace();   
					}
			}
			finally{   
	            // 关闭Connection  
	            con.close();   
			}
			out.println("<script language = javascript>alert('删除成功!');");
			out.println("location.href='Ahomepage.jsp'</script>");
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('删除失败!');");
			out.println("location.href='Ahomepage.jsp'</script>");
		}
	}	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
